import { menu, IMenu } from '__M/menu';
const hasPermission = (value: string | string[], isAnd = false): boolean => {
  // if (value) return true; //开发阶段使用,开发完成需要删除
  let isExist = false;
  const permissions = localStorage.getItem('permissions')
    ? JSON.parse(localStorage.getItem('permissions') || '')
    : [];
  if (!permissions.length) {
    return false;
  }
  if (typeof value === 'string') {
    isExist = permissions.filter((o: string) => o === value).length;
  } else {
    //数组中只要含有一个权限，则显示  isAnd=true : 全部含有才显示
    isExist = isAnd
      ? permissions.every((o: string) => (value as string[])?.includes(o))
      : permissions.some((o: string) => (value as string[])?.includes(o));
  }
  return isExist;
};

//遍历查找第一个存在权限的菜单路径
const checkPermissionPath = (
  permissions: string[],
  menuArr: IMenu[] = menu
): string => {
  let url = '';
  for (let i = 0; i < menuArr.length; i++) {
    const e = menuArr[i];
    if (e.children && e.children.length) {
      url = checkPermissionPath(permissions, e.children);
      if (url) {
        return url;
      }
    } else if (permissions.includes(e.permission)) {
      console.log(e.index, 'indexxxx');
      return e.index;
    }
  }
  console.log(url, 'urlll');
  return url;
};
export { hasPermission, checkPermissionPath };
